package com.example.leaderapplication.View.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.leaderapplication.Base.BaseActivity;
import com.example.leaderapplication.Base.BaseConfig;
import com.example.leaderapplication.Bean.updateUserPassWordRequestBean;
import com.example.leaderapplication.Bean.updateUserPassWordResultBean;
import com.example.leaderapplication.R;
import com.example.leaderapplication.RetrufitHelp.BaseSubscriber;
import com.example.leaderapplication.RetrufitHelp.ExceptionHandle;
import com.example.leaderapplication.RetrufitHelp.RetrofitClient;
import com.example.leaderapplication.Utils.SPUtil;
import com.example.leaderapplication.Utils.StringUtil;
import com.example.leaderapplication.Utils.ToastUtil;

import java.util.Timer;
import java.util.TimerTask;

public class ChangePasswordActivity extends BaseActivity {
    private EditText password, newPassword, reNewPassword;
    private Button changePS;
    private TextView back;
    private updateUserPassWordRequestBean requestBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initView() {
        password = findViewById(R.id.edit_old_password_et);
        newPassword = findViewById(R.id.edit_new_password_et);
        reNewPassword = findViewById(R.id.edit_re_new_password_et);
        changePS = findViewById(R.id.edit_change_password_save);
        back = findViewById(R.id.tv_back);
    }

    @Override
    protected void doSomeThing() {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        changePS.setOnClickListener(new View.OnClickListener() {

            private String trim2;
            private String trim1;
            private String trim;

            @Override
            public void onClick(View v) {

                String oldPs = (String) SPUtil.get(ChangePasswordActivity.this, BaseConfig.PASS_WORD, "");
                trim = password.getText().toString().trim();
                if (StringUtil.isNullOrEmpty(trim)) {
                    ToastUtil.showToast("请输入旧密码");
                    return;
                }
                if (!oldPs.equals(trim)) {
                    ToastUtil.showToast("您输入的旧密码，和原来的不相同");
                    return;
                }

                trim1 = newPassword.getText().toString().trim();
                if (StringUtil.isNullOrEmpty(trim1)) {
                    ToastUtil.showToast("请输入新密码");
                    return;
                }

                trim2 = reNewPassword.getText().toString().trim();
                if (StringUtil.isNullOrEmpty(trim2)) {
                    ToastUtil.showToast("请输入确认密码");
                    return;
                }

                if (!trim1.equals(trim2)) {
                    ToastUtil.showToast("两次输入的新密码不同，请重新输入");
                    return;
                }

                if (trim1.equals(trim)) {
                    ToastUtil.showToast("新密码不能与原密码相同,请重新输入");
                    return;
                }

                updateUserPassWord(password.getText().toString().trim(), newPassword.getText().toString().trim());


            }
        });
    }

    private void updateUserPassWord(String trim, String trim1) {
        requestBean = new updateUserPassWordRequestBean();
        requestBean.setPassword(trim);
        requestBean.setNewPassword(trim1);
        RetrofitClient.getInstance(ChangePasswordActivity.this, baseUrl, headers).updateUserPassWord(requestBean, new BaseSubscriber<updateUserPassWordResultBean>(ChangePasswordActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast("修改密码失败");

            }

            @Override
            public void onNext(updateUserPassWordResultBean updateUserPassWordResultBean) {
                ToastUtil.showToast(updateUserPassWordResultBean.getMessage());
                if (updateUserPassWordResultBean.getCode().equals("200")) {
                    ToastUtil.showToast("修改密码成功，请重新登入");

                    TimerTask task = new TimerTask() {
                        @Override
                        public void run() {
                            startActivity(new Intent(ChangePasswordActivity.this, LoginActivity.class));
                            finish();
                            System.exit(0);
                        }
                    };
                    Timer timer = new Timer();
                    timer.schedule(task, 3000);//3秒后执行TimeTask的run方法

                }

            }
        });

    }
}
